using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using AspNetForums;
using AspNetForums.Components;
using AspNetForums.Controls;
using AspNetForums.Enumerations;

namespace AspNetForums.Admin {
	/// <summary>
	/// Summary description for ForumPermissionAdmin.
	/// </summary>
	public class ForumPermissionAdmin : System.Web.UI.Page {
		protected System.Web.UI.WebControls.Literal lblForumID;
		protected System.Web.UI.WebControls.Literal lblRoleID;
		protected System.Web.UI.WebControls.RadioButtonList optView;
		protected System.Web.UI.WebControls.RadioButtonList optRead;
		protected System.Web.UI.WebControls.RadioButtonList optPost;
		protected System.Web.UI.WebControls.RadioButtonList optReply;
		protected System.Web.UI.WebControls.RadioButtonList optEdit;
		protected System.Web.UI.WebControls.RadioButtonList optDelete;
		protected System.Web.UI.WebControls.RadioButtonList optSticky;
		protected System.Web.UI.WebControls.RadioButtonList optAnnounce;
		protected System.Web.UI.WebControls.RadioButtonList optCreatePoll;
		protected System.Web.UI.WebControls.RadioButtonList optVote;
		protected System.Web.UI.WebControls.RadioButtonList optModerate;
		protected System.Web.UI.WebControls.RadioButtonList optAttachment;
		protected System.Web.UI.WebControls.Button btnSave;
	
		protected ForumPermission _forumPermissionToEdit;

		private void Page_Load(object sender, System.EventArgs e) {
			int roleID = Convert.ToInt32(HttpContext.Current.Request.QueryString["RoleId"]);
			int forumID = ForumContext.Current.ForumID;

			_forumPermissionToEdit = ForumPermissions.GetForumPermission(forumID,roleID);

			AddYesNo(optView);
			AddYesNo(optRead);
			AddYesNo(optPost);
			AddYesNo(optReply);
			AddYesNo(optEdit);
			AddYesNo(optDelete);
			AddYesNo(optSticky);
			AddYesNo(optAnnounce);
			AddYesNo(optCreatePoll);
			AddYesNo(optVote);
			AddYesNo(optModerate);
			AddYesNo(optAttachment);

			// Wire-up buttons
			btnSave.Click	+= new EventHandler(btnSave_Click);

			if( !Page.IsPostBack ){
				DataBind();
			}
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e) {
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent() {    
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		public override void DataBind(){

			Forum forum = Forums.GetForum(_forumPermissionToEdit.ForumID);
			Role role = Roles.GetRole(_forumPermissionToEdit.RoleID);
			
			lblForumID.Text = forum.Name + ": " + _forumPermissionToEdit.ForumID.ToString();
			lblRoleID.Text = role.Name + ": " + _forumPermissionToEdit.RoleID.ToString();

			optView.Items.FindByValue( _forumPermissionToEdit.View == AccessControlEntry.Allow ? "true" : "false"   ).Selected = true;
			optRead.Items.FindByValue( _forumPermissionToEdit.Read == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optPost.Items.FindByValue( _forumPermissionToEdit.Post == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optReply.Items.FindByValue( _forumPermissionToEdit.Reply == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optEdit.Items.FindByValue( _forumPermissionToEdit.Edit == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optDelete.Items.FindByValue( _forumPermissionToEdit.Delete == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optSticky.Items.FindByValue( _forumPermissionToEdit.Sticky == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optAnnounce.Items.FindByValue( _forumPermissionToEdit.Announce == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optCreatePoll.Items.FindByValue( _forumPermissionToEdit.CreatePoll == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optVote.Items.FindByValue( _forumPermissionToEdit.Vote == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optModerate.Items.FindByValue( _forumPermissionToEdit.Moderate == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
			optAttachment.Items.FindByValue( _forumPermissionToEdit.Attachment == AccessControlEntry.Allow ? "true" : "false"  ).Selected = true;
		}

		private void AddYesNo(ListControl listControl) {
			listControl.Items.Add(new ListItem(ResourceManager.GetString("Yes"),"true"));
			listControl.Items.Add(new ListItem(ResourceManager.GetString("No"),"false"));
		}

		private void btnSave_Click(object sender, EventArgs e) {

			int roleID = Convert.ToInt32(HttpContext.Current.Request.QueryString["RoleId"]);
			int forumID = ForumContext.Current.ForumID;

			_forumPermissionToEdit.ForumID		= forumID;
			_forumPermissionToEdit.RoleID		= roleID;
			_forumPermissionToEdit.View			= Boolean.Parse( optView.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Read			= Boolean.Parse( optRead.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Post			= Boolean.Parse( optPost.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Reply		= Boolean.Parse( optReply.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Edit			= Boolean.Parse( optEdit.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Delete		= Boolean.Parse( optDelete.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Sticky		= Boolean.Parse( optSticky.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Announce		= Boolean.Parse( optAnnounce.SelectedItem.Value )	== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.CreatePoll	= Boolean.Parse( optCreatePoll.SelectedItem.Value )	== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Vote			= Boolean.Parse( optVote.SelectedItem.Value )		== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Moderate		= Boolean.Parse( optModerate.SelectedItem.Value )	== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;
			_forumPermissionToEdit.Attachment	= Boolean.Parse( optAttachment.SelectedItem.Value )	== true ? AccessControlEntry.Allow : AccessControlEntry.Deny;

			ForumPermissions.UpdateForumPermission( _forumPermissionToEdit );

			// save is complete, update the page data
			DataBind();
		}

	}
}

